package com.atguigu.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.base.BaseDao;
import com.atguigu.base.BaseServiceImpl;
import com.atguigu.dao.PermissionDao;
import com.atguigu.dao.RolePermissionDao;
import com.atguigu.entity.Permission;
import com.atguigu.entity.RolePermission;
import com.atguigu.helper.PermissionHelper;
import com.atguigu.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 杨林
 * @create 2022-09-29 15:00 星期四
 * description:
 */
@Transactional
@Service(interfaceClass = PermissionService.class)
public class PermissionServiceImpl extends BaseServiceImpl<Permission> implements PermissionService {

    @Autowired
    private PermissionDao permissionDao;

    @Autowired
    private RolePermissionDao rolePermissionDao;

    @Override
    protected BaseDao<Permission> getEntityDao() {
        return permissionDao;
    }

    /**
     * 根据角色id获取授权权限的数据
     * @param roleId
     * @return
     */
    @Override
    public List<Map<String, Object>> findPermissionsByRoleId(Long roleId) {

        //获取全部权限的列表
        List<Permission> permissionList = permissionDao.findAll();

        //获取角色已分配的权限数据的id
        List<Long> permissionIdList = rolePermissionDao.findPermissionIdListByRoleId(roleId);

        //构建zTree数据
        //数据格式： { id:2, pId:0, name:"随意勾选 2", checked:true, open:true}
        ArrayList<Map<String,Object>> zNodes = new ArrayList<>();
        //遍历所有权限  判断该role是否拥有该权限
        for (Permission permission : permissionList) {
            Map<String,Object> map = new HashMap();
            map.put("id",permission.getId());
            map.put("pId", permission.getParentId());
            map.put("name", permission.getName());

            //判断 如果拥有该权限  就将checked属性设置为 true
            if (permissionIdList.contains(permission.getId())){
                map.put("checked", true);
            }
            zNodes.add(map);
        }

        return zNodes;
    }

    /**
     * 保存角色权限
     * @param roleId
     * @param permissionIds
     */
    @Override
    public void saveRolePermissionRealtionShip(Long roleId, Long[] permissionIds) {

        //删除角色原有的权限
        rolePermissionDao.deleteByRoleId(roleId);

        //将permissionIds中不为空的权限加入权限表中
        for (Long permissionId : permissionIds) {
            RolePermission rolePermission = new RolePermission();
            if (permissionId != null){
                rolePermission.setPermissionId(permissionId);
                rolePermission.setRoleId(roleId);
                rolePermissionDao.insert(rolePermission);
            }
        }
    }

    /**
     * 通过用户id获取菜单权限
     * @param adminId
     * @return
     */
    @Override
    public List<Permission> findMenuPermissionByAdminId(Long adminId) {

        //获取用户的权限列表
        List<Permission> permissionList = null; //不包含下级
        //暂时给admin = 1 赋所有权限
        if (adminId == 1){
            permissionList = permissionDao.findAllMenuPermission();
        }else{
            permissionList = permissionDao.findMenuPermissionByAdminId(adminId);
        }

        List<Permission> result = null;  //包含下级
        //将用户的权限列表转变为动态菜单的形式
        result = PermissionHelper.bulid(permissionList);

        return result;
    }

    @Override
    public List<String> findCodeListByAdminId(Long adminId) {
        //获取用户的权限列表
        List<String> codeList = null; //不包含下级
        //暂时给admin = 1 赋所有权限
        if (adminId == 1){
            codeList = permissionDao.findAllCode();
        }else{
            codeList = permissionDao.findCodeList(adminId);
        }
        return codeList;
    }

    @Override
    public List<Permission> findAllMenu() {

        List<Permission> permissionList = permissionDao.findAll();
        if (CollectionUtils.isEmpty(permissionList)){
            return null;
        }
        //构建树型列表
        List<Permission> result = PermissionHelper.bulid(permissionList);
        return result;
    }

    @Override
    public Permission findPermissionByPermissionId(Long id) {
        Permission permission = permissionDao.findPermissionByPermissionId(id);
        return permission;
    }


}
